require.config({
    paths: {
        jquery: '../MoCoop/components/jquery-2.1.4.min',
        MoCoop: '../MoCoop/MoCoop',
        service: '../serviceInterface/serviceInterface',
        Ractive: '../tools/Ractive'
    }
});
require(["jquery","MoCoop",'service','Ractive'], function($,mc,service,Ractive) {
    var userCode;//用户编码
    var profitlossList;//左边的ractve绑定
    var positionList;//右边的ractve绑定
    var currentYear;//保存当前请求的年份
    var availableTime=[];//保存可用时间数组
    var deviceReady = function(){
        mc.getUserInfo(function(){
            userCode=mc.userInfo.userCode;
            eventBind();
            getDateTime();
            mc.showContent();
        });
    };
    var documentReady = function(){
        profitlossList = new Ractive({
            el: "CONTAINER-profitlossList",
            template: "#TEMPLATE-profitlossList",
            data: {listData:[],selectDataList:{list:[],selected:""}}
        });
        positionList = new Ractive({
            el: "CONTAINER-position",
            template: "#TEMPLATE-positionList",
            data: {futureslist:[],selectDataList:{list:[],selected:""},dailyOperation:[]}
        });
    };
    mc.init(deviceReady,documentReady);
    document.addEventListener("refreshEvent",function(){
        currentYear="";//保存当前请求的年份
        availableTime=[];//保存可用时间数组
        getDateTime();
    },false);
    function eventBind(){
        /*tab切换效果*/
        $(".tab-btn").on("click", function () {
            if ($(this).attr("value") == "1") {
                $(".tabContent1").show();
                $(".tabContent2").hide();
            } else {
                $(".tabContent1").hide();
                $(".tabContent2").show();
            }
            $(this).parent().find(".tab-select").removeClass("tab-select");
            $(this).addClass("tab-select");
        });
        /* 时间选择器监听事件1*/
        var profitLossMonth=$("#profitLossMonth");
        var positionMonth=$("#UpTodate");
        switch (mc.device) {
            case "iOS":
                profitLossMonth.val(mc.formatDate(new Date(),"yyyy-MM")).on("blur", function(){
                    var value=$(this).val();
                    getDateTime(mc.formatDate(value,"yyyy-MM"),"left");
                    //getprofitlossList(value);
                });
                positionMonth.val(mc.formatDate(new Date(),"yyyy-MM-dd")).on("blur", function(){
                    var value=$(this).val();
                    getDateTime(mc.formatDate(value,"yyyy-MM"),"right");
                });
                break;
            case "Browser":
            case "Android":
                profitLossMonth.val(mc.formatDate(new Date(),"yyyy-MM")).on("change", function(){
                    var value=$(this).val();
                    getDateTime(mc.formatDate(value,"yyyy-MM"),"left");
                    //getprofitlossList(value);
                });
                positionMonth.val(mc.formatDate(new Date(),"yyyy-MM-dd")).on("change", function(){
                    var value=$(this).val();
                    getDateTime(mc.formatDate(value,"yyyy-MM"),"right");
                });
                break;
        }
        var profitLossDate=$("#profitLossDate");
        var positionDate=$("#positionDate");
        profitLossDate.on("change",function(){
            getprofitlossList(mc.formatDate($(this).val(),"yyyy-MM-dd"));
        });
        positionDate.on("change",function(){
            futurespositions(mc.formatDate($(this).val(),"yyyy-MM-dd"));
        });
    }
    //无数据处理
    var noDataFn=function(type){
        if(type=="left"){
            $(".tb-nodata-left").show();
            $(".tb-total-left").hide();
        }else if(type=="right"){
            $(".tb-nodata-right").show();
            $(".tb-total-right").hide();
        }
    };
    //有数据处理
    var hasDataFn=function(type){
        if(type=="left"){
            $(".tb-nodata-left").hide();
            $(".tb-total-left").show();
        }else if(type=="right"){
            $(".tb-nodata-right").hide();
            $(".tb-total-right").show();
        }
    };
    function getprofitlossList(odate){
        if(odate==undefined||odate==""){
           odate=mc.formatDate(new Date(),"yyyy-MM-dd");
        }
        var option={
            usercode:userCode,
            odate:odate
        };
        var callback=function(data){
            if(data.list==undefined||data.list.length==0){
                profitlossList.data.listData = {};
                profitlossList.update();
                noDataFn("left");
                return;
            }
            hasDataFn("left");
            profitlossList.data.listData = data;
            profitlossList.update("listData");
        };
        mc.notification("加载中","Normal");
        service.getprofitlossList(option,callback);
    }
    function futurespositions(odate){
        if(odate==undefined||odate==""){
            odate=mc.formatDate(new Date(),"yyyy-MM-dd");
        }
        var option={
            usercode:userCode,
            odate:odate
        };
        var callback=function(data){
            if(data.list==undefined||data.list.length==0){
                positionList.data.futureslist = {};
                positionList.update();
                noDataFn("right");
                return;
            }
            hasDataFn("right");
            positionList.data.futureslist = data;
            positionList.update();
        };
        mc.notification("加载中","Normal");
        service.futuresPositionsList(option,callback);
    }
    //获取时间数据
    function getDateTime(date,type){
        var isdefault;//是否是默认状态
        var defaDate;
        var option={
            year:"",
            usercode:userCode
        };

        var goBind=function(){
            var selDate=[];//临时存储日期下拉选项
            //遍历匹配符合条件的日期
            for(var i=0;i<availableTime.length;i++){
                var od=mc.formatDate(new Date(availableTime[i].odate.split(" ")[0]),"yyyy-MM");
                if(od==defaDate){
                    var odate=mc.formatDate(availableTime[i].odate.split(" ")[0],"yyyy-MM-dd");
                    selDate.push({'odate':odate});
                }
            }
            if(selDate.length==0){
                if(type=="left"){
                    profitlossList.data.listData = [];
                    profitlossList.data.selectDataList.list=[];
                    profitlossList.data.selectDataList.selected={};
                    profitlossList.update();
                    noDataFn("left");
                }else if(type=="right"){
                    positionList.data.futureslist = [];
                    positionList.data.selectDataList.list=[];
                    positionList.data.selectDataList.selected={};
                    positionList.update();
                    noDataFn("right");
                }else{
                    profitlossList.data.listData = [];
                    profitlossList.data.selectDataList.list=[];
                    profitlossList.data.selectDataList.selected={};
                    profitlossList.update();
                    positionList.data.futureslist = [];
                    positionList.data.selectDataList.list=[];
                    positionList.data.selectDataList.selected={};
                    positionList.update();
                    noDataFn("left");
                    noDataFn("right");
                }
                return
            }
            //如果是默认模式，则左右两个切换都要请求数据,否则根据type判断
            if(isdefault){
                getprofitlossList(mc.formatDate(availableTime[0].odate.split(" ")[0],"yyyy-MM-dd"));
                futurespositions(mc.formatDate(availableTime[0].odate.split(" ")[0],"yyyy-MM-dd"));
                $("#profitLossMonth").val(mc.formatDate(availableTime[0].odate.split(" ")[0],"yyyy-MM"));
                $("#UpTodate").val(mc.formatDate(availableTime[0].odate.split(" ")[0],"yyyy-MM"));
                profitlossList.data.selectDataList.list = selDate;
                profitlossList.data.selectDataList.selected = selDate[0].odate;
                profitlossList.update("selectDataList");
                positionList.data.selectDataList.list = selDate;
                positionList.data.selectDataList.selected =  selDate[0].odate;
                positionList.update("selectDataList");
            }else if(type=="left"){
                //getprofitlossList(mc.formatDate(availableTime[0].odate.split(" ")[0],"yyyy-MM-dd"));
                getprofitlossList(selDate[0].odate);
                profitlossList.data.selectDataList.list = selDate;
                profitlossList.data.selectDataList.selected = selDate[0].odate;
                profitlossList.update("selectDataList");
            }else if(type=="right"){
                //futurespositions(mc.formatDate(availableTime[0].odate.split(" ")[0],"yyyy-MM-dd"));
                futurespositions(selDate[0].odate);
                positionList.data.selectDataList.list = selDate;
                positionList.data.selectDataList.selected = selDate[0].odate;
                positionList.update("selectDataList");
            }
        };
        var getDataService=function(){
            service.getRequestDate(option,function(data){
                //如果请求成功
                currentYear=option.year;//保存当前请求的年份
                if(data.list!=undefined){
                    availableTime=data.list;//保存当前请求年份的所有数据
                    if(isdefault==true){//如果是第一次请求，则使用第一条有效日期
                        defaDate=mc.formatDate(availableTime[0].odate.split(" ")[0],"yyyy-MM");
                    }
                }else{
                    availableTime=[];
                    //为请求错误处理
                }
                goBind();
            });
        };
        if(currentYear==undefined||currentYear==""){//如果没有已经保存的年份，则表示第一次获取
            option.year=mc.formatDate(new Date(),"yyyy");
            defaDate=mc.formatDate(new Date(),"yyyy-MM");
            isdefault=true;//设置当前为默认模式
            getDataService();
        }else if(currentYear!=mc.formatDate(date,"yyyy")){//如果所请求的年份和当前已保存的年份不符合，则重新请求
            option.year=mc.formatDate(date,"yyyy");
            defaDate=date;
            isdefault=false;//取消默认
            getDataService();
        }else if(currentYear==mc.formatDate(date,"yyyy")){//如果所请求的年份和当前已保存的年份符合
            defaDate=date;
            goBind();
        }
    }
});

